// pages/team/teamList/teamList.js
var g, action_type, picker_val, pages, city_id, m_type_id, app = getApp();
const { lawyerAPI } = require('../../../utils/api');

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		isShow: !1,
		region: ["全国"],
		customItem: '全国',
		heat: !0,
		isLoad: !1,
		index: null,
		picker: ['全部','经济', '民事', '合同','公司改制','婚姻继承'],
		lawersList: [],
		recommendList: [],
		pageNum: 1,
		pageSize: 10,
		hasMore: true,
		loading: false,
		searchKeyword: ''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		g = this;
		g.setData({
			heat: !0
		});
		// 页面加载时同时加载推荐律师和全部律师
		this.loadRecommend();
		this.loadLawyers(true);
	},
	
	RegionChange: function(e) {
		const region = e.detail.value;
		// region为['省','市','区']，只取市或省
		let location = region[1] || region[0] || '全国';
		this.setData({ region: region.length ? region : ["全国"] });
		this.reloadLawyersByFilter(location, this.data.index);
	},
	PickerChange(e) {
		const index = e.detail.value;
		this.setData({ index });
		this.reloadLawyersByFilter(this.data.region[1] || this.data.region[0] || '全国', index);
	},
	heatChange: function() {
		wx.showLoading({
			mask: !0,
			title: "加载中"
		}), wx.getLocation({
			type: "wgs84",
			success: function(t) {
				app.userAddress = t;
				var e = !g.data.heat;
				g.setData({
					heat: e,
					isLoad: !1
				}), wx.hideLoading();
			},
			fail: function(t) {
				wx.hideLoading(), wx.showModal({
					content: "请进入设置打开位置授权",
					title: "提示",
					success: function(t) {
						t.confirm && wx.openSetting({
							success: function(t) {
								t.authSetting["scope.userLocation"] && g
									.heatChange();
							}
						});
					}
				});
			}
		});
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	},

	// 推荐律师（取评分最高的前3名）
	loadRecommend() {
		lawyerAPI.recommend({ strategy: 'rating' }).then(res => {
			// 只取前3名
			this.setData({ recommendList: res.data.slice(0, 3) });
		});
	},
	// 全部律师
	loadLawyers(refresh = false) {
		if (refresh) {
			this.setData({
				pageNum: 1,
				hasMore: true,
				lawersList: []
			});
		}
		if (this.data.loading || (!this.data.hasMore && !refresh)) return;
		this.setData({ loading: true });
		lawyerAPI.list(this.data.pageNum, this.data.pageSize).then(res => {
			const list = refresh ? res.data.list : this.data.lawersList.concat(res.data.list);
			const hasMore = list.length < res.data.total;
			this.setData({
				lawersList: list,
				hasMore,
				loading: false
			});
			if (hasMore && !refresh) {
				this.setData({ pageNum: this.data.pageNum + 1 });
			}
		});
	},
	// 搜索
	onSearchInput(e) {
		this.setData({ searchKeyword: e.detail.value });
	},
	onSearchBtn() {
		if (!this.data.searchKeyword) return;
		lawyerAPI.search({ keyword: this.data.searchKeyword }).then(res => {
			this.setData({ lawersList: res.data, showRecommend: false });
		});
	},
	onClearSearch() {
		this.setData({ searchKeyword: '' });
		this.loadLawyers(true);
	},
	// 跳转详情
	toDetail(e) {
		const id = e.currentTarget.dataset.id;
		wx.navigateTo({ url: '/pages/team/teamDetail/teamDetail?id=' + id });
	},
	// 根据筛选条件重新加载律师
	reloadLawyersByFilter(location, index) {
		// 地域
		let locationFilter = location && location !== '全国' ? location : undefined;
		// 领域
		let specialty = (index && this.data.picker[index] !== '全部') ? this.data.picker[index] : undefined;
		// 推荐律师
		lawyerAPI.recommend({ strategy: 'rating', location: locationFilter, specialty }).then(res => {
			this.setData({ recommendList: res.data.slice(0, 3) });
		});
		// 全部律师
		this.setData({ pageNum: 1, hasMore: true, lawersList: [] });
		lawyerAPI.list(1, this.data.pageSize, { location: locationFilter, specialty }).then(res => {
			this.setData({ lawersList: res.data.list, hasMore: res.data.list.length < res.data.total });
		});
	}
})
